/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mrroman.linksender.sender;

import java.util.Date;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.mrroman.linksender.Configuration;
import com.mrroman.linksender.ioc.In;
import com.mrroman.linksender.ioc.Locales;
import com.mrroman.linksender.ioc.Log;
import com.mrroman.linksender.ioc.Name;

/**
 *
 * @author gorladam
 */
@Name("sender.Pinger")
public class Pinger implements Runnable {

    @Locales
    private ResourceBundle messages;
    @In
    private MessagePoint msgPoint;
    @In
    private Configuration config;
    @In
    private PrivateMessagePoint privateMessagePoint;
    @Log
    private Logger logger;
    private Message msg;
    public static final String PONGMESSAGE = "pong";

    @Override
    public void run() {
        try {
            msg = new Message();
            msg.setFlags(Message.MESSAGE_FLAG_HIDDEN);
            msg.setMessage(PONGMESSAGE);
            logger.info(messages.getString("pinger_start"));
            for (int i = 0; i < 5; i++) {
                Thread.sleep(1000 * 1);
                msg.setSender(config.getNick());
                msg.setDate(new Date());
                msgPoint.sendMessage(msg);
            }
            while (msgPoint.isStarted()) {
                msg.setSender(config.getNick());
                msg.setDate(new Date());
                msgPoint.sendMessage(msg);
                Thread.sleep(1000 * 5);
            }
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
            privateMessagePoint.systemMessage(MessageEvent.EventType.ERROR, "Pinger thread has stopped. Please restart application.\n"+ex.getMessage());
        } finally {
            logger.info(messages.getString("pinger_stop"));
        }
    }
}
